<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'pay';
$return=array();
cfc('core')->checklogin();
$setting=uni_setting('pay');

if($_RQ['op']=='pay'){
	//发起支付时。应当先将订单数据存入$_SESSION['pay']数组中。
	//plugin：订单来源，例如core，则表示系统功能；sz_yi，则表示sz_yi插件。
	//action：订单来源action。
	//id：订单ID
	//status：订单状态，可为空
	//title：订单信息，可为空
	//more：其他信息，可为空
	//其中，id和more字段会在通知支付结果时返回给功能或者插件。
	$id=intval($_RQ['id']);
	if(!$id){
		exi('发起支付失败！','error');
	}
	$pay=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	
	if(!$pay){
		exi('找不到要支付的订单！','','goback');
	}elseif($pay['no_type']){
		$pay['no_type']=json_decode($pay['no_type'],true);
	}
	if($pay['status']){
		exi('订单已支付！','','goback');
	}
	$pay_types=array('hdfk','credit','alipay','wechat','yinlian');//'vip8173888'
	if($pay['no_type']){
		$pay_types=array_diff($pay_types,$pay['no_type']);
	}
	$return=array_fields($pay_types,$setting);
	$return['id']=$pay['id'];
	$return['tid']=$pay['tid'];
	$return['fee']=$pay['fee'];
	$return['credit2']=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'credit2');
	exi($return);
}
if($_RQ['op']=='alipay'){
	
}
if($_RQ['op']=='wechat'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$params=array(
		'title'=>$pay_log['title'],
		'fee'=>$pay_log['fee'],
		'tid'=>$pay_log['tid']
	);
	if($setting['wechat']==1){
		$wechat=$setting;
	}else{
		//借用系统支付
		exi('该公众号暂不支持微信支付！','error');
		//获取粉丝openid
		
	}
	if(!$wechat['wechat_mchid'] || !$wechat['wechat_miyue']){
		exi('支付出错：微信支付参数设置错误！','error');
	}
	$wechat_options=cfc('pay')->wechat_build($params,$wechat);
	if(is_error($wechat_options)){
		exi('支付出错：'.$wechat_options['message'],'error');
	}
	exi($wechat_options);
}
if($_RQ['op']=='wxapp_pay'){
	//小程序支付
	global $_GPC;global $_W;
	$wxgid=intval($_RQ['wxgid']);
	$id=intval($_RQ['orderid']);
	$openid=trim($_RQ['openid']);
	if($_RQ['module_name'] == 'ewei_shopv2'){
		$_RQ['m']='ewei_shopv2';
		$_RQ['r']='app.order.pay.main';
		$_RQ['do']='mobile';
		$_RQ['orderid']=$id;
		$_RQ['comefrom']='wxapp';
		require(ROOT_D.'/api/open_we7/index.php');
		exit;
	}
	$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
	$wechat=array(
		'wechat_appid'=>$wxapp['appid'],
		'wechat_mchid'=>$wxapp['mchid'],
		'wechat_miyue'=>$wxapp['paykey'],
		'openid'=>$openid,
	);
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$params=array(
		'title'=>$pay_log['title'],
		'fee'=>$pay_log['fee'],
		'tid'=>$pay_log['tid']
	);
	$wechat_options=cfc('pay')->wechat_build($params,$wechat);
	if(is_error($wechat_options)){
		exi('支付出错：'.$wechat_options['message'],'error');
	}
	$payinfo=array(
			'message'=>'',
			'type'=>'success',
			'wechat'=>array('success'=>1,'payinfo'=>$wechat_options),
	);
	exi($payinfo);
}
if($_RQ['op']=='credit'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$credit2=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'credit2');
	if($pay_log['fee']>$credit2){
		exi('余额不足！当前账户余额：'.$credit2,'error');
	}
	$credit2=$credit2-$pay_log['fee'];
	pdo_update('core_members',array('credit2'=>$credit2),array('id'=>$_SESSION['uid']));
	pdo_update('core_paylog',array('pay_type'=>'credit','status'=>1),array('tid'=>$pay_log['tid']));
	$log=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'type'=>2,
		'is_add'=>0,
		'fee'=>$pay_log['fee'],
		//'result'=>$credit2,
		'remark'=>'支付订单：'.$pay_log['title'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_credit_log',$log);
	exi('success');
}
if($_RQ['op']=='yinlian'){
	
}
if($_RQ['op']=='pay_result'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$plugin_type=pdo_getcolumn('core_plugins',array('name'=>$log['plugin']),'type');
	if($post['status']='success'){
		$update=array('pay_type'=>$post['type'],'status'=>1);
		$message='支付成功！';		
		$result=array('id'=>$log['oid'],'status'=>1,'pay_type'=>$post['type'],'fee'=>$log['fee'],'pay_id'=>$log['tid'],'title'=>$log['title']);
		$_SESSION['payresult']=$result;
	}else{
		$update=array('pay_type'=>$post['type'],'status'=>2);
		$message='支付失败！';
		$result=array('id'=>$log['oid'],'status'=>2,'pay_type'=>$post['type'],'fee'=>$log['fee'],'pay_id'=>$log['tid'],'title'=>$log['title']);
		$_SESSION['payresult']=$result;
	}
	pdo_update('core_paylog',$update,array('id'=>$id));
	if($plugin_type!=4){
		$url=$log['back_url']?$log['back_url']:'goback';
		exi($message,'',$url);
	}else{
		exi($message,'','./index.php?i='.$_SESSION['uniacid'].'&m='.$log['plugin'].'&action=module&do=payresult');
	}
}
if($_RQ['op']=='wxapp_result'){
	global $_GPC;global $_W;
	$post=get_postdata();
	$id=intval($post['orderid']);
	$openid=trim($post['openid']);
	$_RQ['r_url']=$post['r_url'];
	$_RQ['type']=$post['type'];
	$_RQ['openid']=$openid;
	$_RQ['orderid']=$id;
	if($post['module_name'] == 'ewei_shopv2'){
		$_RQ['m']='ewei_shopv2';
		$_RQ['r']='app.order.pay.complete';
		$_RQ['do']='mobile';
		$_RQ['id']=$id;
		require(ROOT_D.'/api/open_we7/index.php');
		exit;
	}else{
		exi('success');
	}
}